/**
 * 权限声明：
 */
package com.handy.webwork.view;

import java.util.HashMap; 
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 资源文件类，存储共用资源与插件资源。
 * 
 * @author rocken.zeng@gmail.com
 * 
 */
public class MessageResource {
	private static Log log = LogFactory.getLog(MessageResource.class);
	private static MessageResource instance = null;//实例
	//存储共用资源
	private HashMap<String, String> messageRessource = new HashMap<String, String>();
	//存储插件资源
	private HashMap<String, HashMap<String, String>> bundleMessageRessource = new HashMap<String, HashMap<String, String>>();

	/*
	 * 构造函数私有化，该类不充许直接构造。 
	 */
	private MessageResource(){}
	
	/**
	 * 获取MessageResource实例。
	 * @return
	 */
	public static synchronized MessageResource getInstance() {
		if (instance == null) {
			instance = new MessageResource();
		}
		return instance;
	}

	/**
	 * 获取共用资源。
	 * @param key 资源键值
	 * @return
	 */
	public String getText(String key) {
		//log.info("invoke getText('"+key+"')");
		return messageRessource.get(key.toUpperCase()) == null ? ""
				: messageRessource.get(key.toUpperCase());
	}
	
	/**
	 * 获取插件资源。
	 * @param bundle 插件名称
	 * @param key 资源键值
	 * @return
	 */
	public String getText(String bundle, String key) {
		//log.info("invoke getText('"+bundle+"','"+key+"')");
		if (bundle == null || "".equals(bundle)) {
			return getText(key);
		} else {
			if (bundleMessageRessource.get(bundle.toUpperCase()) != null) {
				return bundleMessageRessource.get(bundle.toUpperCase()).get(
						key.toUpperCase()) == null ? ""
						: bundleMessageRessource.get(bundle.toUpperCase()).get(
								key.toUpperCase());
			}
		}
		return "";
	}
	
	/**
	 * 加入公共资源。
	 * @param key 资源键值
	 * @param value 资源内容
	 */
	public static void putText(String key, String value) {
		getInstance().messageRessource.put(key, value);
	}
	
	/**
	 * 加入插件资源。
	 * @param bundle 插件名
	 * @param key 资源键值
	 * @param value 资源内容
	 */
	public static void putText(String bundle,String key, String value) {
		if (bundle==null || "".equals(bundle)) return;
		if (getInstance().bundleMessageRessource.get(bundle.toUpperCase())==null){
			getInstance().bundleMessageRessource.put(bundle.toUpperCase(), new HashMap<String, String>());
		}
		
		getInstance().bundleMessageRessource.get(bundle.toUpperCase()).put(key, value);
	}
	
	/**
	 * 清空所有资源。
	 */
	public void clear(){
		if (messageRessource!=null && !messageRessource.isEmpty()) messageRessource.clear();
		if (bundleMessageRessource!=null && !bundleMessageRessource.isEmpty()){
			for (Map.Entry<String, HashMap<String, String>> entry : bundleMessageRessource.entrySet()) {
				if (entry.getValue()!=null && !entry.getValue().isEmpty()) entry.getValue().clear();
			}
			bundleMessageRessource.clear();
		}
	}

	/**
	 * 打印测试。
	 */
	public void print() {
		log.info("=======================================");
		log.info("共用资源详单：");
		if (messageRessource!=null && !messageRessource.isEmpty()){
			for (Map.Entry<String, String> entry : messageRessource.entrySet()) {
				String _key = (String) entry.getKey();
				String _value = (String) entry.getValue();
				log.info(_key + " = " + _value);
			}
		} else {
			log.info("无可用共用资源");
		}
		log.info("=======================================");
		log.info("插件资源详单：");
		if (bundleMessageRessource!=null && !bundleMessageRessource.isEmpty()){
			for (Map.Entry<String, HashMap<String, String>> entry : bundleMessageRessource.entrySet()) {
				log.info("=============");
				log.info("插件名称："+entry.getKey());
				log.info("=============");
				if (entry.getValue()!=null && !entry.getValue().isEmpty()){
					HashMap<String, String> bundles = entry.getValue();
					for (Map.Entry<String, String> entry1 : bundles.entrySet()) {
						String _key = (String) entry1.getKey();
						String _value = (String) entry1.getValue();
						log.info(_key + " = " + _value);
					}	
				}else {
					log.info("该插件无可用资源");
				}
			}
		}else{
			log.info("无可用插件资源");
		}
		log.info("=======================================");
		
	}

}